[XENOPROF] Fix limit-check overflow.
authorkfraser@localhost.localdomain <kfraser@localhost.localdomain>
Sun, 15 Oct 2006 08:52:33 +0000 (09:52 +0100)
committerkfraser@localhost.localdomain <kfraser@localhost.localdomain>
Sun, 15 Oct 2006 08:52:33 +0000 (09:52 +0100)
Fix code limiting XENOPROF_get_buffer and XENOPROF_set_passive
argument max_samples so that no more than MAX_OPROF_SHARED_PAGES are
used.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
xen/arch/x86/oprofile/xenoprof.c

index 4379d1223b933889d4344aac6ca9990aa16b04d5..0eaa0f71a1f77644ebc49ac7064e915e6286ae7a 100644 (file)
@@ -122,6 +122,7 @@ int alloc_xenoprof_struct(struct domain *d, int max_samples, int is_passive)
 {
     struct vcpu *v;
     int nvcpu, npages, bufsize, max_bufsize;
+    unsigned max_max_samples;
     int i;
 
     d->xenoprof = xmalloc(struct xenoprof);
@@ -139,17 +140,15 @@ int alloc_xenoprof_struct(struct domain *d, int max_samples, int is_passive)
     for_each_vcpu ( d, v )
         nvcpu++;
 
-    /* reduce buffer size if necessary to limit pages allocated */
-    bufsize = sizeof(struct xenoprof_buf) +
-        (max_samples - 1) * sizeof(struct event_log);
+    /* reduce max_samples if necessary to limit pages allocated */
     max_bufsize = (MAX_OPROF_SHARED_PAGES * PAGE_SIZE) / nvcpu;
-    if ( bufsize > max_bufsize )
-    {
-        bufsize = max_bufsize;
-        max_samples = ( (max_bufsize - sizeof(struct xenoprof_buf)) /
+    max_max_samples = ( (max_bufsize - sizeof(struct xenoprof_buf)) /
                         sizeof(struct event_log) ) + 1;
-    }
+    if ( (unsigned)max_samples > max_max_samples )
+        max_samples = max_max_samples;
 
+    bufsize = sizeof(struct xenoprof_buf) +
+        (max_samples - 1) * sizeof(struct event_log);
     npages = (nvcpu * bufsize - 1) / PAGE_SIZE + 1;
     
     d->xenoprof->rawbuf = alloc_xenoprof_buf(is_passive ? dom0 : d, npages);